package com.senac.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.senac.model.Aluno;

public class AlunoDao {
	
	public List<Aluno> getAlunos() {		
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Aluno> list = new ArrayList<Aluno>();
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM ALUNO");
			rs = ps.executeQuery();
			while(rs.next()) {
				Aluno aluno = new Aluno();
				aluno.setId(rs.getInt("ID"));
				aluno.setNome(rs.getString("NOME"));
				aluno.setRg(rs.getString("RG"));
				aluno.setCargaHoraria(rs.getInt("CARGA_HORARIA"));
				list.add(aluno);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}				
		return list;
	}
	
	
	

	public Aluno getAlunosById(Integer id) {
		Connection con =  null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Aluno aluno = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("SELECT * FROM ALUNO WHERE ID = ?");
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if(rs.next()) {
				aluno = new Aluno();
				aluno.setId(rs.getInt("ID"));
				aluno.setNome(rs.getString("NOME"));
				aluno.setRg(rs.getString("RG"));
				aluno.setCargaHoraria(rs.getInt("CARGA_HORARIA"));	
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			BDUtil.finalizeConnection(rs, ps, con);
		}				
		return aluno;
	}

	
	
	public boolean save(Aluno aluno) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("INSERT INTO ALUNO (nome, carga_horaria, rg) VALUES (?, ?, ?)");			
			ps.setString(1, aluno.getNome());
			ps.setInt(2, aluno.getCargaHoraria());
			ps.setString(3, aluno.getRg());
			return (ps.executeUpdate() > 0);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
	}

	
	
	
	public boolean update(Aluno aluno) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("UPDATE ALUNO SET nome = ?, carga_horaria = ?, rg = ? WHERE ID = ?");
			ps.setString(1, aluno.getNome());
			ps.setInt(2, aluno.getCargaHoraria());
			ps.setString(3, aluno.getRg());
			ps.setInt(4, aluno.getId());
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}

	}

	
	public boolean delete(Integer id) {
		Connection con =  null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			ps = con.prepareStatement("DELETE FROM ALUNO WHERE ID = ?");			
			ps.setInt(1, id);
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			BDUtil.finalizeConnection(ps, con);
		}
	}
	

}
